package date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;

/**
 * @description: 格式化或解析日期,类似SimpleDateFormat
 * @author: apple
 * @date:  2021/12/11 8:23 下午
 * @version 1.0
 **/
public class DateTimeFormatterTest {

    public static void main(String[] args) {

        // 方式一
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

        // 日期转字符串
        String str = formatter.format(LocalDateTime.now());

        System.out.println(str);

        TemporalAccessor d = formatter.parse("2021-12-11T20:30:31.513");
        System.out.println(d);


        // 方式二
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String str2 = dateTimeFormatter.format(LocalDateTime.now());
        System.out.println(str2);

        // 方式三
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String str3 = dateTimeFormatter1.format(LocalDate.now());
        System.out.println(str3);

        // 方式四
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String str4 = dateTimeFormatter2.format(LocalDateTime.now());
        System.out.println(str4);
        TemporalAccessor parse = dateTimeFormatter2.parse("2021-12-11 09:04:59");
        System.out.println(parse);



    }
}
